#!/bin/bash
clear

TEXT='\033[30;107;2m'
INFO='\033[97;44m'
WARNING='\033[97;43m'
ERROR='\033[97;41m'
SUCCESS='\033[97;42m'
GREEN='\033[92m'
YELLOW='\033[93m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
    echo -e "${ERROR} ERROR ${RESET}${TEXT} You need to run this script as root! ${RESET}"; exit 1
fi

function installPkg {
    clear
    read -r -p "Continue with the installation? [Y/n] " _yesno
    if [[ "$_yesno" =~ ^[Yy]$ ]]; then
        if [[ ! -d /etc/openvpn ]]; then
            wget -q -O - https://raw.githubusercontent.com/cybertize/doctype/default/packages/ovpn.sh | bash
        fi
        systemctl stop openvpn
        apt-get -y -qq --purge remove openvpn &>/dev/null
        apt-get -y -qq autoremove &>/dev/null
        apt-get -qq autoclean &>/dev/null
        clear && sleep 3
        wget -q -O - https://raw.githubusercontent.com/cybertize/doctype/default/packages/ovpn.sh | bash
    else
        echo -e "${ERROR} ERROR ${RESET}${TEXT} Aborted... ${RESET}"; exit 1
    fi
}

function uninstallPkg {
    clear
    read -r -p "Are you sure you want to uninstall openvpn? [Y/n] " _yesno
    if [[ "$_yesno" =~ ^[Yy]$ ]]; then
        apt-get -y -qq --purge remove openvpn &>/dev/null
        apt-get -y -qq autoremove &>/dev/null
        apt-get -qq autoclean &>/dev/null

        echo -e "${SUCCESS} SUCCESS ${RESET}${TEXT} Purge & Removing openvpn package ${RESET}"; exit 0
    else
        echo -e "${ERROR} ERROR ${RESET}${TEXT} Aborted... ${RESET}"; exit 1
    fi
}

function configurePkg {
    clear
    server-tcp() {
        local tcp_port=$(cat /etc/openvpn/server-tcp.conf | grep -w 'port' | cut -d ' ' -f 2)

        until [[ -n "$input_tcp_port" && "$input_tcp_port" =~ ^[0-9]+$ ]]; do
            read -p "Enter new port: " input_tcp_port
            sed -i "s|$tcp_port|$input_tcp_port|g" /etc/openvpn/server-tcp.conf
            systemctl restart openvpn@server-tcp
        done
    }
    server-udp() {
        local udp_port=$(cat /etc/openvpn/server-udp.conf | grep -w 'port' | cut -d ' ' -f 2)

        until [[ -n "$input_udp_port" && "$input_udp_port" =~ ^[0-9]+$ ]]; do
            read -p "Enter new port: " input_udp_port
            systemctl stop openvpn@server-udp
            sed -i "s|$udp_port|$input_udp_port|g" /etc/openvpn/server-udp.conf
            systemctl start openvpn@server-udp
        done
    }
    server-tls() {
        local tls_port=$(cat /etc/openvpn/server-tls.conf | grep -w 'port' | cut -d ' ' -f 2)

        until [[ -n "$input_tls_port" && "$input_tls_port" =~ ^[0-9]+$ ]]; do
            read -p "Enter new port: " input_tls_port
            systemctl stop openvpn@server-tls
            systemctl stop stunnel4

            sed -i "s|$tls_port|$input_tls_port|g" /etc/openvpn/server-tls.conf
            sed -i "s|connect = 127.0.0.1:$tls_port|connect = 127.0.0.1:$input_tls_port|g" /etc/stunnel/stunnel.conf
            systemctl start openvpn@server-tls
            systemctl start stunnel4
        done
    }

    echo
    echo -e "${TEXT} OPENVPN CONFIG FILE MENU                                   ${RESET}"
    echo
    echo -e "[01] ${GREEN}tcp${RESET}   - ${YELLOW}/etc/openvpn/server-tcp.conf${RESET}"
    echo -e "[02] ${GREEN}udp${RESET}   - ${YELLOW}/etc/openvpn/server-udp.conf${RESET}"
    echo -e "[03] ${GREEN}tls${RESET}   - ${YELLOW}/etc/openvpn/server-tls.conf${RESET}"
    echo -e "[00] ${GREEN}back${RESET}  - ${YELLOW}Exit from config file menu${RESET}"
    echo
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo
    while true; do
        read -p "Which config you want to edit: " _choice
        case "$_choice" in
            01|tcp)
                server-tcp && break
            ;;
            02|udp)
                server-udp && break
            ;;
            03|tls)
                server-tls && break
            ;;
            00|back)
                openvpnMenu && break
            ;;
            *)
                echo; echo -e "${ERROR} ERROR ${RESET}${TEXT} Aborted... ${RESET}"; echo
                configurePkg
            ;;
        esac
    done
}

function detailPkg {
    clear
    local unit_id=$(systemctl show openvpn.service -p Id | cut -d '=' -f 2)
    local unit_name=$(systemctl show openvpn.service -p Names | cut -d '=' -f 2)
    local unit_desc=$(systemctl show openvpn.service -p Description | cut -d '=' -f 2)
    local is_active=$(systemctl is-active openvpn.service)
    local is_enable=$(systemctl is-enabled openvpn.service)
    local tcp_port=$(cat /etc/openvpn/server-tcp.conf | grep -w 'port' | cut -d ' ' -f 2)
    local udp_port=$(cat /etc/openvpn/server-udp.conf | grep -w 'port' | cut -d ' ' -f 2)
    local tls_port=$(cat /etc/openvpn/server-tls.conf | grep -w 'port' | cut -d ' ' -f 2)

    echo
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo -e "${TEXT} Openvpn service detail                                     ${RESET}"
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo
    echo -e "${YELLOW}     ID:${RESET} ${GREEN}$unit_id${RESET}"
    echo -e "${YELLOW}   Name:${RESET} ${GREEN}$unit_name${RESET}"
    echo -e "${YELLOW}   Desc:${RESET} ${GREEN}$unit_desc${RESET}"
    echo -e "${YELLOW} Status:${RESET} ${GREEN}$is_active & $is_enable${RESET}"
    echo -e "${YELLOW}  Ports:${RESET} ${GREEN}$tcp_port(tcp) $udp_port(udp) $tls_port(tls) ${RESET}";
    echo
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo -e "${TEXT} Created by Doctype, Powered by Cybertize.                  ${RESET}"
    echo -e "${TEXT} Copyright 2021, Allright Reserved.                         ${RESET}"
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo
}

function openvpnMenu {
    clear; echo
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo -e "${TEXT} OPENVPN MENU                                               ${RESET}"
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo
    echo -e "[01] ${GREEN}reinstall${RESET}  - ${YELLOW}Reinstall openvpn package${RESET}"
    echo -e "[02] ${GREEN}uninstall${RESET}  - ${YELLOW}Uninstall openvpn package${RESET}"
    echo -e "[03] ${GREEN}configure${RESET}  - ${YELLOW}Edit/Configure openvpn config${RESET}"
    echo -e "[04] ${GREEN}detail   ${RESET}  - ${YELLOW}Show openvpn detail & status${RESET}"
    echo -e "[00] ${GREEN}quit     ${RESET}  - ${YELLOW}Exit from menu${RESET}"
    echo
    echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
    echo -e "${TEXT} Created by Doctype, Powered by Cybertize.                  ${RESET}"
    echo -e "${TEXT} Copyright 2021, Allright Reserved.                         ${RESET}"
    echo -e "${TEXT} ========================================================== ${RESET}"
    echo
    while true; do
        read -p "Enter your choice: " _choice
        case "$_choice" in
            01|reinstall)
                installPkg && break
            ;;
            02|uninstall)
                uninstallPkg && break
            ;;
            03|configure)
                configurePkg && break
            ;;
            04|detail)
                detailPkg && break
            ;;
            00|quit)
                exit 0
            ;;
            *)
                echo -e "${ERROR} ERROR ${RESET}${TEXT} Invalid option! ${RESET}"; openvpnMenu
            ;;
        esac
    done
}

if [[ -d /etc/openvpn ]]; then
    openvpnMenu
elif [[ ! -d /etc/openvpn ]]; then
    installPkg
fi
